﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.TcCommands.TcReplaceJt.CUiTcReplaceJt
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.CodeDom.Compiler;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Markup;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui.WPF;


namespace DnProcessSimulateCommands.TcCommands.TcReplaceJt;

public class CUiTcReplaceJt : TxWindow, IComponentConnector
{
  private CApTcReplaceJtViewModel m_viewModel;
  internal System.Windows.Controls.ListView m_instances;
  internal System.Windows.Controls.TextBox m_currentJtName;
  internal System.Windows.Controls.TextBox m_newJtPath;
  internal System.Windows.Controls.Button btnBrowse;
  internal System.Windows.Controls.Button btnReplace;
  internal System.Windows.Controls.Button btnCancel;
  private bool _contentLoaded;

  public CUiTcReplaceJt(CApTcReplaceJtModel model)
  {
    this.InitializeComponent();
    this.m_viewModel = new CApTcReplaceJtViewModel(model);
    ((FrameworkElement) this).DataContext = (object) this.m_viewModel;
  }

  private void btnCancel_Click(object sender, RoutedEventArgs e)
  {
    ((Window) this).DialogResult = new bool?(false);
    ((Window) this).Close();
  }

  private void TxWindow_Loaded(object sender, RoutedEventArgs e)
  {
    this.m_newJtPath.GetBindingExpression(System.Windows.Controls.TextBox.TextProperty).UpdateSource();
    this.m_newJtPath.Focus();
  }

  private void btnReplace_Click(object sender, RoutedEventArgs e)
  {
    string errors;
    if (this.IsValid(out errors))
    {
      ((Window) this).DialogResult = new bool?(true);
      ((Window) this).Close();
    }
    else
    {
      if (string.IsNullOrEmpty(errors))
        return;
      int num = (int) TxMessageBox.ShowModal(errors, TcReplaceJtStringTable.CMD_NAME, MessageBoxButtons.OK, MessageBoxIcon.Hand);
      // ISSUE: explicit non-virtual call
      __nonvirtual (((UIElement) this).Focus());
    }
  }

  private void btnBrowse_Click(object sender, RoutedEventArgs e)
  {
    using (OpenFileDialog openFileDialog = new OpenFileDialog())
    {
      string directoryName = Environment.SpecialFolder.Desktop.ToString();
      string str = string.Empty;
      object obj = (object) null;
      TxWindowSettings.RestoreWindowValue((TxWindow) this, "LastJtPath", ref obj);
      string fileName = obj != null ? obj.ToString() : string.Empty;
      if (fileName != string.Empty)
      {
        FileInfo fileInfo = new FileInfo(fileName);
        if (fileInfo.Directory != null && fileInfo.Directory.Exists)
        {
          str = fileInfo.Name;
          directoryName = fileInfo.DirectoryName;
        }
      }
      openFileDialog.Filter = TcReplaceJtStringTable.OPEN_FILE_FILTER;
      openFileDialog.Title = TcReplaceJtStringTable.OPEN_FILE_TITLE;
      openFileDialog.FileName = str;
      openFileDialog.InitialDirectory = directoryName;
      if (openFileDialog.ShowDialog() != DialogResult.OK)
        return;
      this.m_viewModel.NewJtPath = openFileDialog.FileName;
      TxWindowSettings.StoreWindowValue((TxWindow) this, "LastJtPath", (object) openFileDialog.FileName);
    }
  }

  private bool IsValid(out string errors)
  {
    bool flag = true;
    StringBuilder stringBuilder = new StringBuilder();
    if (!this.IsValidNewJtPath())
    {
      flag = false;
      stringBuilder.AppendLine(TcReplaceJtStringTable.ERROR_INVALID_JT_FILE_PATH);
      this.m_newJtPath.Focus();
    }
    errors = stringBuilder.ToString();
    return flag;
  }

  private bool IsValidNewJtPath()
  {
    bool flag = false;
    string newJtPath = this.m_viewModel.NewJtPath;
    if (!string.IsNullOrWhiteSpace(newJtPath) && newJtPath.EndsWith(".jt", StringComparison.InvariantCultureIgnoreCase) && File.Exists(newJtPath))
      flag = true;
    return flag;
  }

  private void m_instances_SizeChanged(object sender, SizeChangedEventArgs e)
  {
    System.Windows.Controls.ListView instances = this.m_instances;
    (instances.View as GridView).Columns[0].Width = instances.ActualWidth;
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  public void InitializeComponent()
  {
    if (this._contentLoaded)
      return;
    this._contentLoaded = true;
    System.Windows.Application.LoadComponent((object) this, new Uri("/DnProcessSimulateCommands;component/tccommands/tcreplacejt/cuitcreplacejt.xaml", UriKind.Relative));
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  [EditorBrowsable(EditorBrowsableState.Never)]
  void IComponentConnector.Connect(int connectionId, object target)
  {
    switch (connectionId)
    {
      case 1:
        ((FrameworkElement) target).Loaded += new RoutedEventHandler(this.TxWindow_Loaded);
        break;
      case 2:
        this.m_instances = (System.Windows.Controls.ListView) target;
        this.m_instances.SizeChanged += new SizeChangedEventHandler(this.m_instances_SizeChanged);
        break;
      case 3:
        this.m_currentJtName = (System.Windows.Controls.TextBox) target;
        break;
      case 4:
        this.m_newJtPath = (System.Windows.Controls.TextBox) target;
        break;
      case 5:
        this.btnBrowse = (System.Windows.Controls.Button) target;
        this.btnBrowse.Click += new RoutedEventHandler(this.btnBrowse_Click);
        break;
      case 6:
        this.btnReplace = (System.Windows.Controls.Button) target;
        this.btnReplace.Click += new RoutedEventHandler(this.btnReplace_Click);
        break;
      case 7:
        this.btnCancel = (System.Windows.Controls.Button) target;
        this.btnCancel.Click += new RoutedEventHandler(this.btnCancel_Click);
        break;
      default:
        this._contentLoaded = true;
        break;
    }
  }
}
